<template>
  <div class="container">
    <div class="title">
      <span>添加</span>
      <span class="back" @click="back">
        <i class="iconfont icon-fanhui"></i> 返回
      </span>
    </div>
    <el-divider></el-divider>
    <el-form label-width="120px">
      <div class="wrap">
        <el-form-item label="活动标题" prop="name">
          <el-input
                  size="medium"
                  v-model="title"
                  placeholder="请填写活动标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <upload-imgs
            ref="uploadEle"
            :value="imgValue"
            :rules="rules"
            :multiple="true"
            :sortable="true"
            :min-num="1"
            :max-num="5"
          />
        </el-form-item>
        <el-form-item label="排序" prop="order_sort">
          <el-input
            size="medium"
            type="number"
            v-model="order_sort"
            placeholder="请填写排序(值越大越在前)"
          ></el-input>
        </el-form-item>
        <el-form-item label="活动周期">
          <el-checkbox-group v-model="weekData">
              <el-checkbox label="星期日" key="0"></el-checkbox>
              <el-checkbox label="星期一" key="1"></el-checkbox>
              <el-checkbox label="星期二" key="2"></el-checkbox>
              <el-checkbox label="星期三" key="3"></el-checkbox>
              <el-checkbox label="星期四" key="4"></el-checkbox>
              <el-checkbox label="星期五" key="5"></el-checkbox>
              <el-checkbox label="星期六" key="6"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="活动报名价格" prop="price">
          <el-input
                  size="medium"
                  type="number"
                  v-model="price"
                  placeholder="请填写活动报名价格"
          ></el-input>
        </el-form-item>
        <el-form-item label="活动时间">
            <el-button
                type="primary"
                plain
                @click="addTime"
            >添加活动时间</el-button
            >
        </el-form-item>
        <el-form-item v-for="(item, index) in dateArr" :key="index">
          <el-time-select
              v-model="item.start_time"
              :picker-options="{
                start: '06:00',
                step: '00:10',
                end: '23:00'
              }"
              placeholder="活动开始时间">
          </el-time-select>
          -
          <el-time-select
              v-model="item.end_time"
              :picker-options="{
                start: '06:00',
                step: '00:10',
                end: '23:00'
              }"
              placeholder="活动结束时间">
          </el-time-select>
          <el-button
              type="danger"
              plain
              size="mini"
              @click="delTime(index, item)"
              style="margin-left: 20px"
              v-if="index > 0"
          >删除</el-button
          >
        </el-form-item>
        <el-form-item label="活动报名人数">
          <el-radio v-model="num_status" label="0">无限制</el-radio>
          <el-radio v-model="num_status" label="1">人数限制</el-radio>
          <el-radio v-model="num_status" label="2">性别限制</el-radio>
        </el-form-item>
        <el-form-item label="报名人数" prop="number" v-if="num_status === '1'">
          <el-input
                  size="medium"
                  v-model="number"
                  type="number"
                  placeholder="报名人数"
          ></el-input>
        </el-form-item>
        <el-form-item label="男生报名人数" prop="men_num" v-if="num_status === '2'">
          <el-input
                  size="medium"
                  type="number"
                  v-model="men_num"
                  placeholder="男生报名人数"
          ></el-input>
        </el-form-item>
        <el-form-item label="女生报名人数" prop="women_num" v-if="num_status === '2'">
          <el-input
                  size="medium"
                  type="number"
                  v-model="women_num"
                  placeholder="女生报名人数"
          ></el-input>
        </el-form-item>
        <el-form-item label="活动介绍">
          <div class="lin-wrap">
            <tinymce :defaultContent="content" @change="change" />
          </div>
        </el-form-item>
        <el-form-item label="注意事项">
          <el-input type="textarea" v-model="zy_content"></el-input>
        </el-form-item>
        <el-form-item class="submit">
          <el-button type="primary" @click="createData()">提 交</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import SjActivity from '@/models/sj-activity'
import UploadImgs from '@/components/base/upload-imgs'
import Tinymce from '@/components/base/tinymce'

export default {
  components: {
    UploadImgs,
    Tinymce,
  },
  props: {
    orderSort: {
      type: Number,
    },
    sjMsgId: {
      type: Number,
    },
  },
  data() {
    return {
      imgValue: [],
      dateArr: [
        {
          start_time: '',
          end_time: '',
        },
      ],
      weekData: [],
      title: '',
      order_sort: '',
      price: '',
      num_status: '0',
      number: '',
      men_num: '',
      women_num: '',
      content: '',
      zy_content: '',
      rules: {
        maxSize: 0.4,
      },
    }
  },
  mounted() {
    this.order_sort = this.orderSort
  },
  methods: {
    delTime(index) {
      this.dateArr = this.dateArr.slice(this.dateArr, index)
    },
    addTime() {
      this.dateArr.push({
        start_time: '',
        end_time: '',
      })
    },
    back() {
      this.$emit('addClose')
    },
    async createData() {
      const self = this
      const imgValue = await this.$refs.uploadEle.getValue()
      const img_ids = []
      imgValue.forEach((item) => {
        img_ids.push({ imgId: item.imgId })
      })
      const info = {
        img_ids,
        sj_msg_id: self.sjMsgId,
        title: self.title,
        week_data: self.weekData,
        order_sort: self.order_sort,
        price: self.price,
        num_status: self.num_status,
        number: self.number,
        men_num: self.men_num,
        women_num: self.women_num,
        content: self.content,
        zy_content: self.zy_content,
        time_arr: self.dateArr,
      }
      const res = await SjActivity.createData(info)
      if (res.error_code === 0) {
        self.imgValue = []
        this.$message.success(`${res.msg}`)
        this.back()
      }
    },
    change(val) {
      this.content = val
    },
  },
}
</script>

<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 0;
}

.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
</style>
